package com.youxue.teachin.update;

import java.io.File;

import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.view.View;
import com.ali.music.navigator.backstack.Params;
import com.library.uiframework.activity.BaseActivity;
import com.library.uiframework.utils.StringUtils;
import com.youxue.teachin.common.utils.PreferencesUtils;
import me.drakeet.materialdialog.MaterialDialog;

/**
 * 在此写用途
 *
 * @author: zhiwei
 * @date: 2017-10-30 21:28
 * @version: 9.1.0
 */
public class UpdateDilogActivity extends BaseActivity {
    private MaterialDialog alert;
    String apkUrl = null;
    String versinName;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Params params = getParams();

        versinName = params.getString("versionName", "");
        String update_log = params.getString("update_log", "是否更新？");
        apkUrl = params.getString("apk_url", "");
        if (StringUtils.isEmpty(apkUrl)) {
            finish();
            return;
        }
        String message = "最新版本：" + versinName
            + "\n\n"
            + "更新内容：\n"
            + update_log;

        alert = new MaterialDialog(this);
        alert.setTitle("发现新版本")
            .setMessage(message)
            .setCanceledOnTouchOutside(true)
            .setPositiveButton("立即更新", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    alert.dismiss();
                    alert = null;
                    download();
                    finish();
                }
            })
            .setNegativeButton("稍后提醒", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    alert.dismiss();
                    alert = null;
                    finish();
                }
            })
            .setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialog) {
                    alert = null;
                    finish();
                }
            }).show();
    }

    void download() {
        File file = new File(new File(Environment.getExternalStorageDirectory(), "TeachIN"), "apk");
        if (!file.exists()) {
            file.mkdirs();
        }
        File saveFile = new File(file, "TeachIN-" + versinName + ".apk");
       long downloadId = DownloadUtils.downloadFile(this.getApplicationContext(), apkUrl, saveFile.getAbsolutePath(), "TeachIN下载中",
            "TeachIN下载最新版本：" + versinName);
        PreferencesUtils.saveString("downloadId", String.valueOf(downloadId));
        PreferencesUtils.saveString("downloadFile", saveFile.getAbsolutePath());
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    @Override
    protected void onDestroy() {
        if (alert != null) {
            alert.dismiss();
        }
        super.onDestroy();
    }
}
